/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.restaurant;

import com.supinfo.youfood.entity.Menu;
import com.supinfo.youfood.entity.Restaurant;
import com.supinfo.youfood.service.MenuService;
import com.supinfo.youfood.service.RestaurantService;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "DefineMenuDayServlet", urlPatterns = {"/admin/definir-menu-jour"})
public class DefineMenuDayServlet extends HttpServlet {
    
    @EJB
    private RestaurantService restoService;
    
    @EJB
    private MenuService menuService;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {        
        List<Restaurant> restos = restoService.getAllResto();
        request.setAttribute("restos", restos);
        
        List<Menu> menus = menuService.getAllMenus();
        request.setAttribute("menus", menus);
        
        request.getRequestDispatcher("/jsp/admin/definir-menu-jour.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        Restaurant resto = restoService.findRestoById(new Long(request.getParameter("resto")));
        Menu menu = menuService.findMenuById(new Long(request.getParameter("menu")));
        
        resto.setPlatDuJour(menu);
        
        restoService.refreshResto(resto);
        
        response.sendRedirect("/YouFood/admin/accueil");
    }

}
